package com.zwc.angle2048;

/**
 * 工具类
 * 
 * @author zwc
 *
 */
public class GameUtil {

	public static final int WIDTH = 480;
	public static final int HEIGHT = 854;

	/**
	 * 高分榜排序
	 * 
	 * @param oldStr
	 * @return
	 */
	public static String[] isHiScore(String[] oldStr) {
		String[] newStr = new String[6];
		int[][] hiScore = new int[6][3];
		int num = 5;
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 3; j++) {
				hiScore[i][j] = Integer.parseInt(toStrArr(oldStr[i])[j]);
			}
		}
		for (int i = 4; i >= 0; i--) {
			if ((hiScore[5][0] > hiScore[i][0])
					|| ((hiScore[5][0] == hiScore[i][0]) && (hiScore[5][1] < hiScore[i][1]))
					|| ((hiScore[5][0] == hiScore[i][0])
							&& (hiScore[5][1] == hiScore[i][1]) && (hiScore[5][2] < hiScore[i][2]))) {
				num--;
			}
		}
		newStr[5] = oldStr[5];
		for (int n = 0; n < 5; n++) {
			if (n < num) {
				newStr[n] = oldStr[n];
			} else if (n == num) {
				newStr[n] = oldStr[5];
			} else if (n > num) {
				newStr[n] = oldStr[n - 1];
			}
		}
		return newStr;
	}

	public static String[] toStrArr(String str) {
		String[] s = str.split("_");
		return s;
	}

	/**
	 * 将记录4x4卡片数值的数组转换为字符串 用"_"分割
	 * 
	 * @param num
	 * @return
	 */
	public static String tostring(int[][] num) {
		String str = "";
		for (int x = 0; x < 4; x++) {
			for (int y = 0; y < 4; y++) {
				if (x == 3 && y == 3)
					str += (num[x][y]);
				else
					str += (num[x][y] + "_");
			}
		}
		return str;
	}

	/**
	 * 将字符串转换为4x4卡片数值的数组
	 * 
	 * @param str
	 * @return
	 */
	public static int[][] tonum(String str) {
		int i = 0;
		int[][] num = new int[4][4];
		String[] strs = str.split("_");
		for (int x = 0; x < 4; x++) {
			for (int y = 0; y < 4; y++) {
				num[x][y] = Integer.parseInt(strs[i]);
				i++;
			}
		}
		return num;
	}

	/**
	 * 将秒数转换为时间格式的字符串
	 * 
	 * @param time
	 * @return
	 */
	public static String secToTime(int time) {
		String timeStr = null;
		int hour = 0;
		int minute = 0;
		int second = 0;
		if (time <= 0)
			return "00:00";
		else {
			minute = time / 60;
			if (minute < 60) {
				second = time % 60;
				timeStr = format(minute) + ":" + format(second);
			} else {
				hour = minute / 60;
				if (hour > 99)
					return "99:59:59";
				minute = minute % 60;
				second = time - hour * 3600 - minute * 60;
				timeStr = format(hour) + ":" + format(minute) + ":"
						+ format(second);
			}
		}
		return timeStr;
	}

	public static String format(int i) {
		String retStr = null;
		if (i >= 0 && i < 10)
			retStr = "0" + Integer.toString(i);
		else
			retStr = "" + i;
		return retStr;
	}

}
